<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Home\Controller;
use Think\Controller;

class MemberController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	/**
	 * 用户列表
	 *
	 **/
	public function lists(){
		$page = I('get.page') ? (int)I('get.page') : 1;
		$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
		$count = D('Member')->getMemberListCount();
		//用户列表
		$userlist = D('Member')->getMemberList($page);
		
		$this->assign('userlist', $userlist);
		$this->assign('count', $count);
		$this->assign('page', $page);
		//判断请求方式
		if(!$ajax){
			$this->display('lists');
		}else{
			$this->display('lists_page', 'utf-8', 'html');
		}
	}
	
	/**
	 * 导出用户数据为Excel格式
	 *
	 ***/
	public function excel(){
		$list = D('Member')->getMemberListAll();
		$xlsCell = array(
			array('member_nick', '客人姓名'),
			array('member_card','身份证'),
			array('member_phone','联系方式'),
			array('room_number','房间号'),
			array('visible','状态'),
			array('code','验证码'),
			array('checkin_time','入住时间'),
			array('leaving_time','退房时间'),
			array('nickname','操作员'),
			array('createtime','添加时间'),
		);
		if(is_array($list)){
			foreach($list as $ls){
				$excelData[] = array(
					'member_nick' => $ls['member_nick'],
					'member_card' => $ls['member_card'],
					'member_phone' => $ls['member_phone'],
					'room_number' => $ls['room_number'],
					'visible' => $ls['visible'] == 1 ? '租用中' : '无效',
					'code' => $ls['code'],
					'checkin_time' => date('Y-m-d H:i', $ls['checkin_time']),
					'leaving_time' => date('Y-m-d H:i', $ls['leaving_time']),
					'nickname' => $ls['nickname'],
					'createtime' => date('Y-m-d H:i', $ls['createtime'])
				);
			}
		}
		//执行
		D('Excel')->exportExcel('客人信息表', $xlsCell, $excelData, '客人信息表_'.date('Y-m-d H:i'));			
	}
	
	/**
	 * 删除客人
	 **/
	public function remove(){
		if(IS_POST){
			$user_id = I('user_id');
			$state = I('post.status') ? (int)I('post.status') : 0;
			
			if($user_id <= 0 ){
				$this->ajaxReturn(0, 'json');
			}else{
				if(D('Member')->delMemberInfo($user_id, $state) !== false){
					$this->ajaxReturn($user_id, 'json');
				}else{
					$this->ajaxReturn(0, 'json');
				}
			}
		}
	}
	
	/**
	 * 添加编辑客人信息
	 *
	 **/
	public function add(){
		if(IS_GET){
			$member_id = I('get.member_id') ? (int)I('get.member_id') : 0;
			if($member_id){
				$info = D('Member')->getMemberInfoById($member_id);
				$this->assign('userinfo', $info);
			}
			$this->display();
		}else if(IS_POST){
			$member_id = I('post.member_id') ? (int)I('post.member_id') : 0;
			$data = array(
				'member_nick' => I('post.member_nick') ? trim(I('post.member_nick')) : '',
				'member_card' => I('post.member_card') ? trim(I('post.member_card')) : '',
				'room_number' => I('post.room_number') ? trim(I('post.room_number')) : '',
				'member_phone' => I('post.member_phone') ? trim(I('post.member_phone')) : '',
				'checkin_time' => I('post.checkin_time'),
				'leaving_time' => I('post.leaving_time')
			);
			
			//开始判断参数
			if(empty($data['member_nick'])){
				$this->ajaxReturn('请输入客人名字', 'json');
				exit;
			}
			
			if(empty($data['member_card'])){
				$this->ajaxReturn('请输入客人身份证', 'json');
				exit;
			}
			
			if(empty($data['member_phone'])){
				$this->ajaxReturn('请输入客人联系方式', 'json');
				exit;
			}
			
			if(empty($data['room_number'])){
				$this->ajaxReturn('请输入房间号', 'json');
				exit;
			}
			$times = '';
			if(empty($data['checkin_time'])){
				$this->ajaxReturn('请选择入住时间', 'json');
				exit;
			}else{
				$times = $data['checkin_time'];
				$data['checkin_time'] = strtotime($data['checkin_time']);
			}
			
			if(empty($data['leaving_time'])){
				$this->ajaxReturn('请选择离开时间', 'json');
				exit;
			}else{
				$times = $times .'~'. $data['leaving_time'];
				$data['leaving_time'] = strtotime($data['leaving_time']);
			}
			
			//获取房间对应的ID
			$room = D('Room')->getRoomInfoByNumber($data['room_number']);
			if(is_array($room) && count($room)){
				$data['room_id'] = $room['room_id'];
			}else{
				$this->ajaxReturn('不存在该房间', 'json');
			}
			
			//开始保存数据
			if($member_id){
				$row = D('Member')->editMemberInfo($member_id, $data);
			}else{
				$data['createtime'] = time();
				$data['code'] = rand(100000, 999999);
				$data['user_id'] = session('user_id');
				
				$row = D('Member')->addMemberInfo($data);
				$type = I('post.type') ? trim(I('post.type')) : '';
				$floor = I('post.floor') ? (int)I('post.floor') : 1;
				$roomtype = I('post.roomtype') ? trim(I('post.roomtype')) : '';
				
				//发送短信验证码
				if($row !== false){
					$this->sendCode($data['member_phone'], $data['code'], $data['member_nick'], $type, $data['room_number'], $floor, $roomtype, $times);
				}
			}
			
			if($row !== false){
				$this->ajaxReturn('操作成功', 'json');
			}else{
				$this->ajaxReturn('操作失败', 'json');
			}
		}
	}
	
	public function test(){
		echo $this->sendCode('15823214287', '652365','刘强','娃哈哈','103',1,'豪华型','2017-01-05 00:00~2017-01-06 14:00');
	}
	
	/**
	 * 发送短信验证码
	 * @param string $phone
	 * @param int $code
	 **/
	private function sendCode($phone, $code, $name='', $type='',$room_number='', $floor=1, $roomtype='', $times=''){
		$smsurl = 'http://m.5c.com.cn/api/send/index.php?';
		
		$message = '尊敬的 '.$name.' 宾客，您好！您已成功预订嘛嘛度假公寓博鳌店客房。您选择的是'.$type.'，房间号为'. $room_number .'，位于'. $floor .'楼，房型为'. $roomtype .'，入住时间为'. $times .'。您的自助办理钥匙的授权码为：'. $code .'(请保存)\r\n　　公寓地址：海南省琼海市博鳌镇东山街62号美丽熙海岸3栋嘛嘛公寓。公寓管家热线：（0898）62609040（24小时服务）。若后续有任何订房疑问，可联系13350383372。嘛嘛公寓，永远贴心陪伴您。';
		$param = array(
			'username' => 'mmgy',
			'password_md5' => md5('mamagongyu2015'),
			'apikey' => 'cd983820e908517d1c8b6fb9762a5ae3',
			'mobile' => $phone,
			'content' => urlencode($message),
			'encode' => 'UTF-8'
		);
		
		$paramstring = http_build_query($param);
		$content = file_get_contents($smsurl. $paramstring);
		if(strpos($content, 'success') >= 0){
			return true;
		}else{
			return false;
		}
	}
}


   